/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.stat.model.selection;

import org.stat.model.Model;
import org.stat.model.comparison.ModelErrComparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author stewari1
 */
class ModelSelection {

    /** Field description */
    private final List<Model> models = new ArrayList<>();

    /** Field description */
    private boolean modelsOrdered = false;

    /** Field description */
    private ModelErrComparator modelErrComparator;

    /**
     * Constructs ...
     */
    public ModelSelection() {}

    public void setModelErrComparator(ModelErrComparator modelErrComparator) {

        this.modelErrComparator = modelErrComparator;
        this.modelsOrdered      = false;
    }

    public void addModel(Model model) {

        this.models.add(model);

        this.modelsOrdered = false;
    }

    public void removeModel(Model model) {

        this.models.remove(model);

        this.modelsOrdered = false;
    }

    Model[] getOrderModels() {

        if (!this.modelsOrdered) {

            Collections.sort(this.models, this.modelErrComparator);

            this.modelsOrdered = true;
        }

        return this.models.toArray(new Model[0]);
    }

    public Model getBestModel() {
        return getOrderModels()[0];
    }
}
